package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);

        ShoppingCart shoppingCart1=shoppingCartService.addShoppingCard(shoppingCart);
        return R.success(shoppingCart);
    }
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        List<ShoppingCart> shoppingCartList=shoppingCartService.list(userId);
        return R.success(shoppingCartList);
    }
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCartService.clean(userId);
        return R.success("删除购物车！");
    }

    //删除购物车中的商品
//    @PostMapping("/sub")
//    public R<String> sub(@RequestBody Map<String, Long> params){
//        Long dishId = params.get("dishId");
//        Long setmealId = params.get("setmealId");
//        shoppingCartService.sub(dishId,setmealId);
//        return R.success("成功删除购物车商品");
//    }
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCart1=shoppingCartService.sub(shoppingCart);
        return R.success(shoppingCart);
    }
}
